package com.chinafree.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chinafree.auth.mapping.LoginUserMapping;
import com.chinafree.auth.model.bo.LoginUserBo;
import com.chinafree.auth.model.enumeration.Column;
import com.chinafree.auth.service.LoginUserService;
import com.chinafree.common.utils.MD5Utils;
import com.chinafree.mapper.MaUserMapper;
import com.chinafree.mapper.UserRoleMapper;
import com.chinafree.model.po.MaUser;
import com.chinafree.permission.model.po.UserRolePO;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 对数据库的操作
 */
@Service
public class LoginUserServiceImpl implements LoginUserService {

    @Autowired
    private MaUserMapper maUserMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public Optional<LoginUserBo> getLoginUserByLoginMail(String loginMail) {
        QueryWrapper<MaUser> queryWrapper = new QueryWrapper<MaUser>();
        queryWrapper.eq(Column.LOGIN_MAIL.getColumn(), loginMail);
        queryWrapper.eq(Column.IS_DELETED.getColumn(), 0);
        MaUser sysLoginUser = maUserMapper.selectOne(queryWrapper);
        return initLoginUserRef(sysLoginUser);
    }

    @Override
    public Optional<LoginUserBo> getUserId(Long userId) {
        QueryWrapper<MaUser> queryWrapper = new QueryWrapper<MaUser>();
        queryWrapper.eq(Column.ID.getColumn(), userId);
        MaUser sysLoginUser = maUserMapper.selectOne(queryWrapper);
        return initLoginUserRef(sysLoginUser);
    }

    @Override
    public Optional<LoginUserBo> getLoginUserByLoginMobile(String phone) {
        QueryWrapper<MaUser> queryWrapper = new QueryWrapper<MaUser>();
        queryWrapper.eq(Column.PHONE.getColumn(), phone);
        queryWrapper.eq(Column.IS_DELETED.getColumn(), 0);
        MaUser sysLoginUser = maUserMapper.selectOne(queryWrapper);
        return initLoginUserRef(sysLoginUser);
    }

    @Override
    public Optional<LoginUserBo> modifyPasswordById(Long LoginUserId, String password) {
        MaUser maUser = MaUser.builder().password(MD5Utils.hash(password)).build();
        maUser.setId(LoginUserId);
        maUserMapper.updateById(maUser);
        MaUser user = maUserMapper.selectById(LoginUserId);
        return initLoginUserRef(user);
    }

    /**
     * 初始化用户映射信息
     *
     * @param loginUser
     */
    private Optional<LoginUserBo> initLoginUserRef(MaUser loginUser) {
        if (loginUser == null) {
            return Optional.empty();
        }
        LoginUserBo loginUserBo = LoginUserMapping.instance.maUser2Bo(loginUser);
        return Optional.of(loginUserBo);
    }

    @Override
    public Integer getRoleIds(Integer id, Long userId) {
        QueryWrapper<UserRolePO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(Column.USER_ID.getColumn(), userId);
        List<UserRolePO> userRolePOS = userRoleMapper.selectList(queryWrapper);
        List<Integer> collect = userRolePOS.stream().map(UserRolePO::getRoleId).filter(u -> id.equals(u))
                .collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(collect)) {
            return collect.get(0);
        }
        return null;
    }
}
